import '../dao/dao_wordbook.dart';
import '../model/model_user_word.dart';
import '../tools/tools_subtitle.dart';

class ServiceUserWord {
  static void saveRightSentence(String sentence) {
    // 1. 得到句子中所有单词
    List<String> words = ToolsSubtitle.strToWords(sentence);
    // 2. 遍历保存单词
    for (String w in words) {
      add(w, true);
    }
  }

  static void saveWrongSentence(String sentence, List<String> inputs) {
    // 1. 得到句子中所有单词
    List<String> words = ToolsSubtitle.strToWords(sentence);

    // 2. 保存错误单词
    for (String w in words) {
      if (!inputs.contains(w)) {
        add(w, false);
      }
    }
  }

  // 添加单词
  static Future<void> add(String word, bool right) async {
    // 1. 查询单词
    ModelUserWord? w = await DaoUserWord.findByWord(word);

    // 2. 已有则更新
    if (w != null) {
      if (right) {
        // 更新正确
        w.inputRightQty++;
        if (w.type == 1) {
          // 如果是熟词 其他不做修改
        } else if (w.type == 0) {
          // 如果是生词
          // 更新复习时间
          // 更新复习进度
          if (w.startReviewTime == null || w.startReviewTime == 0) {
            w.startReviewTime = DateTime.now().millisecondsSinceEpoch;
          }
          w.reviewProgress = await w.calcReviewProgress();
          w.lastReviewTime = DateTime.now().millisecondsSinceEpoch;
        } else if (w.type == -1) {
          // 如果是忽略词 其他不做修改
        }
      } else {
        // 更新错误
        w.inputWrongQty++;
        if (w.type == 1) {
          // 如果熟词  改为生词
          w.type = 0;
          w.startReviewTime = 0;
          w.reviewProgress = 0;
          w.lastReviewTime = 0;
        } else if (w.type == 0) {
          // 如果是生词 重置复习时间
          w.startReviewTime = 0;
          w.reviewProgress = 0;
          w.lastReviewTime = 0;
        } else if (w.type == -1) {
          // 如果是忽略词 不做修改
        }
      }
      DaoUserWord.updateById(w);
    }
    // 3. 没有则插入
    else {
      ModelUserWord? w = ModelUserWord();
      w.word = word;
      w.writeWrongQty = 1;
      if (right) {
        // 如果正确  插入为熟词
        w.type = 1;
      } else {
        // 如果错误 插入为生词
        w.type = 0;
      }
      DaoUserWord.insert(w);
    }
  }

  // 修改单词类型
  static Future<void> modifiy(String word, int type) async {
    ModelUserWord? w = await DaoUserWord.findByWord(word);
    if (w != null) {
      w.type = type;
      DaoUserWord.updateById(w);
    }
  }

  // 列表单词
  static Future<List<ModelUserWord>> listAll() async {
    List<ModelUserWord> list = await DaoUserWord.findByQueryString(" order by gmt_modified desc");
    return list;
  }

  static Future<int> countAll() async {
    return await DaoUserWord.countByQueryString("");
  }

  static Future<int> countNew() async {
    return await DaoUserWord.countByQueryString(" where type=0");
  }

  static Future<int> countNeedReview() async {
    List<ModelUserWord> list = await DaoUserWord.findByQueryString(" where type = 0 order by gmt_modified desc");
    List<ModelUserWord> list2 = [];
    for (var i = 0; i < list.length; i++) {
      if (await list[i].needReview()) {
        list2.add(list[i]);
      }
    }
    return list2.length;
  }

  // 列表生词
  static Future<List<ModelUserWord>> listNew() async {
    List<ModelUserWord> list = await DaoUserWord.findByQueryString(" where type = 0 order by gmt_modified desc");
    return list;
  }

  // 列表需要复习的
  static Future<List<ModelUserWord>> listNeedReview() async {
    List<ModelUserWord> list = await DaoUserWord.findByQueryString(" where type = 0 order by gmt_modified desc");
    List<ModelUserWord> list2 = [];
    for (var i = 0; i < list.length; i++) {
      if (await list[i].needReview()) {
        list2.add(list[i]);
      }
    }
    return list2;
  }
}
